package app;

import app.controllers.*;
import app.database.InitDatabase;
import app.models.*;
import app.database.MainDatabase;
import framework.spatial.FRDSpatialMapImageBackground;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

/**
 *
 * @author Lukáš Marek
 */
public class MainWindow extends javax.swing.JFrame
{
	private MainDatabase db;
	private TemporalController tc;
	private MultimediaController mc;
	private SpatialController sc;

	/**
	 * Vytvoří nové okno MainWindow
	 */
	public MainWindow(MainDatabase database)
	{
		initComponents();
		db = database;
		
		//Multimedialni část
		fRDMultimedia1.setModel(new MultimediaModel(db));
		mc = new MultimediaController(fRDMultimedia1, fRDMultimedia1.getModel());
		mc.loadItems();

		//Prostorová část
		fRDSpatial1.setModel(new SpatialModel(db));
		fRDSpatial1.setMapSize(612, 500);//velikost souradnicoveho systemu urcuje velikost obrazku v pixelech
		sc = new SpatialController(fRDSpatial1, fRDSpatial1.getModel());
		sc.setBackgroundClass(new FRDSpatialMapImageBackground(getClass().getResource("/data/map.jpg")));
		try
		{
			sc.setDefaultPointImage(ImageIO.read(getClass().getResource("/data/symbol_cave.png")));
		}
		catch (IOException e)	{ System.err.println(e.getMessage()); }
		sc.loadItems();
		
		//Temporální část
		fRDTemporal1.setModel(new TemporalModel(db));
		tc = new TemporalController(fRDTemporal1, fRDTemporal1.getModel());
		tc.loadItems();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents()
  {

    jTabbedPane1 = new javax.swing.JTabbedPane();
    fRDMultimedia1 = new framework.multimedia.FRDMultimedia();
    fRDSpatial1 = new framework.spatial.FRDSpatial();
    fRDTemporal1 = new framework.temporal.FRDTemporal();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenuFile = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();
    jMenuExit = new javax.swing.JMenuItem();
    jMenuHelp = new javax.swing.JMenu();
    jMenuAbout = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jTabbedPane1.addTab("Multimediální", fRDMultimedia1);
    jTabbedPane1.addTab("Prostorová", fRDSpatial1);
    jTabbedPane1.addTab("Temporální", fRDTemporal1);

    jMenuFile.setText("Soubor");

    jMenuItem1.setText("Nahrát testovací data do databáze");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        jMenuItem1ActionPerformed(evt);
      }
    });
    jMenuFile.add(jMenuItem1);

    jMenuExit.setText("Ukončit");
    jMenuExit.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        jMenuExitActionPerformed(evt);
      }
    });
    jMenuFile.add(jMenuExit);

    jMenuBar1.add(jMenuFile);

    jMenuHelp.setText("Nápověda");

    jMenuAbout.setText("O aplikaci");
    jMenuAbout.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        jMenuAboutActionPerformed(evt);
      }
    });
    jMenuHelp.add(jMenuAbout);

    jMenuBar1.add(jMenuHelp);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1080, Short.MAX_VALUE)
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(0, 75, Short.MAX_VALUE))
    );

    jTabbedPane1.getAccessibleContext().setAccessibleDescription("");

    pack();
  }// </editor-fold>//GEN-END:initComponents

	/**
	 * zavreni aplikace
	 * @param evt 
	 */
  private void jMenuExitActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuExitActionPerformed
  {//GEN-HEADEREND:event_jMenuExitActionPerformed
    db.Logout();
		setVisible(false);
		dispose();
  }//GEN-LAST:event_jMenuExitActionPerformed

	/**
	 * zobrazi dialog o aplikaci
	 * @param evt 
	 */
  private void jMenuAboutActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuAboutActionPerformed
  {//GEN-HEADEREND:event_jMenuAboutActionPerformed
    JOptionPane.showMessageDialog(this, "Ukázková aplikace pro diplomovou práci\n"
																			+ "Rámec pro rychlý vývoj GUI klientských aplikací post-relačních databází\n"
																			+ "Autor: Lukáš Marek",
																			"O aplikaci",JOptionPane.INFORMATION_MESSAGE);
  }//GEN-LAST:event_jMenuAboutActionPerformed

  private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jMenuItem1ActionPerformed
  {//GEN-HEADEREND:event_jMenuItem1ActionPerformed
		int n = JOptionPane.showOptionDialog(this,
    "Budou smazány všechny data a nahrána nová",
    "Nahrání testovacích dat do databáze",
    JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
		
		if (n == 0)
		{
			(new InitDatabase(db)).start();
			mc.loadItems();
			fRDMultimedia1.repaint();
			sc.loadItems();
			fRDSpatial1.repaint();
			tc.loadItems();
			fRDTemporal1.repaint();
		}
  }//GEN-LAST:event_jMenuItem1ActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private framework.multimedia.FRDMultimedia fRDMultimedia1;
  private framework.spatial.FRDSpatial fRDSpatial1;
  private framework.temporal.FRDTemporal fRDTemporal1;
  private javax.swing.JMenuItem jMenuAbout;
  private javax.swing.JMenuBar jMenuBar1;
  private javax.swing.JMenuItem jMenuExit;
  private javax.swing.JMenu jMenuFile;
  private javax.swing.JMenu jMenuHelp;
  private javax.swing.JMenuItem jMenuItem1;
  private javax.swing.JTabbedPane jTabbedPane1;
  // End of variables declaration//GEN-END:variables
}
